package com.mayikt;

import java.util.concurrent.*;

/**
 * @Author: maplefire
 * @Description: Callable和Future示例
 * @Date: 2022/09/02 18:03
 */
class TaskCallable implements Callable<String> {

    public String call() throws Exception {
        System.out.println("1.执行任务开始..");
        Thread.sleep(5000);
        System.out.println("2.执行任务结束..");
        return "success";
    }
}


public class Test01 {
    public static void main(String[] args) {
        ExecutorService executorService = Executors.newCachedThreadPool();
        final Future<String> submit = executorService.submit(new TaskCallable());
        System.out.println("3.主线程开始执行");
        new Thread(new Runnable() {
            public void run() {
                try {
                    String result = submit.get();
                    System.out.println("4.获取到future结果：" + result);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        System.out.println("5.主线程继续执行");
    }
}
